package com.ai.ly.goodsautocat.autocat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.ai.ly.goodsautocat.common.Constants;
import com.ai.ly.goodsautocat.util.FileUtil;
import com.ai.ly.goodsautocat.vo.CatInfo;

public class CatMap {
    
    /**
     * 商品分类，key=分类id，value=分类名称
     */
    private static Map<String, String> catIdNameMap=new HashMap<>();
    
    /**
     * 商品分类，key=分类是第几位，value=分类id
     */
    private static Map<Integer, String> catIndexIdMap=new HashMap<>();
    
    /**
     * 商品分类，key=分类id，value=分类是第几位
     */
    private static Map<String,Integer> catIdIndexMap=new HashMap<>();
    
    static final String filePath="data\\原始数据\\原始数据(类型ID-类型名称).txt";
    
    private CatMap() {
        try {
            File file=FileUtil.getFileInSys(filePath);
            BufferedReader e = new BufferedReader(new FileReader(file));
            int index=1;
            String lineContent=null;
            while ((lineContent = e.readLine()) != null) {
                
                char s = lineContent.trim().charAt(0);
                // 65279是空字符
                if (s == 65279) {
                    if (lineContent.length() > 1) {
                        lineContent = lineContent.substring(1);
                    }
                }
                
                int commaIndex=lineContent.lastIndexOf(Constants.SPLIT_COMMA);
                String catId=StringUtils.trimToEmpty(lineContent.substring(0, commaIndex));
                String catName=StringUtils.trimToEmpty(lineContent.substring(commaIndex+1,lineContent.length()));
                
                catIdNameMap.put(catId, catName);
                catIndexIdMap.put(index, catId);
                catIdIndexMap.put(catId, index);
                
                index++;
            }
            e.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
    private static CatMap instance=new CatMap();
    public static CatMap getInstance() {
        return instance;
    }

    public static CatInfo getCatInfoByIndex(int index) {
        String catId=catIndexIdMap.get(index);
        if (catId==null) {
            return null;
        }
        String catName=catIdNameMap.get(catId);
        CatInfo vo=new CatInfo();
        vo.setCatId(catId);
        vo.setCatName(catName);
        vo.setIndex(index);
        return vo;
    }
    
    public static CatInfo getCatInfoByCatId(String catId) {
        if (catId==null) {
            return null;
        }
        String catName=catIdNameMap.get(catId);
        
        CatInfo vo=new CatInfo();
        vo.setCatId(catId);
        vo.setCatName(catName);
        vo.setIndex(getIndexByCatId(catId));
        return vo;
    }
    
    public static int getIndexByCatId(String catId) {
        Integer index=catIdIndexMap.get(catId);
        if (index==null) {
            return Integer.MIN_VALUE;
        }
        return index;
    }
    
    public static void main(String[] args) {
        System.out.println(catIdNameMap.get("6"));
        System.out.println(catIndexIdMap.get(1));
        
        Integer tmp=catIdIndexMap.get("6");
        System.out.println(tmp);
        
        String catId="0";
        System.out.println(getIndexByCatId(catId));
        
        int index=1;
        System.out.println(getCatInfoByIndex(index));
    }
}
